package com.example.dell.fastshopping.user.setting;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.loginRegist.ForgetAndResetPassword;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.user.UserInfo.UserinfoActivity;
import com.example.dell.fastshopping.user.address.AddressManagerActivity;
import com.example.dell.fastshopping.utils.CacheDataManager;
import com.example.dell.fastshopping.utils.KLog;
import com.example.dell.fastshopping.utils.SPDao;
import com.example.dell.fastshopping.views.CommentItem;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends AppCompatActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.address_manager)
    CommentItem addressManager;
    @BindView(R.id.outlogin)
    TextView outlogin;
    @BindView(R.id.touser_info)
    CommentItem touserInfo;
    @BindView(R.id.safe_setting)
    CommentItem safeSetting;
    @BindView(R.id.right_text)
    TextView rightText;
    @BindView(R.id.cache)
    TextView cache;
    @BindView(R.id.clear)
    RelativeLayout clear;
    @BindView(R.id.tel)
    TextView tel;
    private SettingViewMolder molder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        molder = ViewModelProviders.of(this).get(SettingViewMolder.class);
        title.setText("设置");
        String totalCacheSize = CacheDataManager.getTotalCacheSize(this);
        cache.setText(totalCacheSize);
    }


    @OnClick({R.id.back, R.id.address_manager, R.id.tel, R.id.about,
            R.id.outlogin, R.id.touser_info, R.id.safe_setting, R.id.clear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                back();
                break;
            case R.id.address_manager:
                toActivity(AddressManagerActivity.class);
                break;
            case R.id.outlogin:
                loginOut();
                break;
            case R.id.safe_setting:
                Intent intent = new Intent(this, ForgetAndResetPassword.class);
                startActivity(intent);
                break;
            case R.id.touser_info:
                goUserBaseInfo();
                break;
            case R.id.clear:
                CacheDataManager.clearAllCache(this);
                String totalCacheSize = CacheDataManager.getTotalCacheSize(this);
                cache.setText(totalCacheSize);
                break;
            case R.id.tel:
                tel();
                break;
            case R.id.about:
                toAboutAvtivity();
                break;
        }
    }

    private void toAboutAvtivity() {
        Intent intent = new Intent(this, AboutActivity.class);
        startActivity(intent);
    }

    private void tel() {
        Intent intentPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + tel.getText().toString().trim()));
        startActivity(intentPhone);
    }


    private void goUserBaseInfo() {
        Intent intent = new Intent(this, UserinfoActivity.class);
        startActivity(intent);
    }

    private void back() {
        finish();
    }

    private void loginOut() {
        molder.loginOut().observe(this, new Observer<BaseResponse>() {
            @Override
            public void onChanged(@Nullable BaseResponse response) {
                KLog.e(response);
                if (response.isSuccess()) {
                    SPDao.setShouldLogin(true);
                    back();
                }
            }
        });
    }

    private void toActivity(Class<AddressManagerActivity> addressManagerClass) {
        Intent intent = new Intent(this, addressManagerClass);
        startActivity(intent);
    }

}
